﻿#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.31.186"));
    mqtt->setPort(1883);
    mqtt->connectToHost();
    //实例化四个界面
    log = new login;
    fir = new first;
    stm = new stm32;
    mod = new modbus;
    set = new setting;
    vi = new VideoWidget;
    dev = new Widget;
    //将四个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(log);
    ui->stackedWidget->addWidget(fir);
    ui->stackedWidget->addWidget(stm);
    ui->stackedWidget->addWidget(mod);
    ui->stackedWidget->addWidget(set);
    ui->stackedWidget->addWidget(vi);
    ui->stackedWidget->addWidget(dev);

    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));


   // 哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     stm, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     mod, SLOT(dataUpSlot(QByteArray)));
}

HomePage::~HomePage()
{
    delete ui;
}
